#include "displayTab.h"
#include "mesinkata.h"
#include <conio.h>


char NextChar (char s)
/* mengembalikan karakter berikutnya dari s */
/* Jika s = 'Z', maka akan dikembalikan dengan 'A' */
{
	int i;
	i = s;
	i++;
	if (i > 90)
	{
		i = 65;
	}
	return(i);
}

void NextAlfa (string *s)
/* mencari alfabet berikutnya dari dari alfabet sekarang */
{
	int i;
	boolean Z;
	
	if ((*s).s[(*s).length-1] != 'Z')
	{
		(*s).s[((*s).length) - 1] = NextChar((*s).s[((*s).length) - 1]);
	}
	else
	{
		i = 0;
		Z = true;
		while (i < (*s).length)
		{
			if ((*s).s[i] != 'Z')
			{
				Z = false;
			}
			i++;
		}
				
		if (Z == true)
		{
			(*s).s[(*s).length] = 'A';
			for (i = (*s).length - 1; i >= 0; i--)
			{
				(*s).s[i] = NextChar((*s).s[i]);
			}
			(*s).length = (*s).length + 1;
		}
		else
		{
			(*s).s[(*s).length-1] = 'A';
			i = (*s).length - 2;
			while ((i >= 0) && ((*s).s[i+1] == 'A'))
			{
				(*s).s[i] = NextChar((*s).s[i]);
				i--;
			}
		}
	}
	(*s).s[(*s).length]='\0';
}

void PC(char c , int x)
/* mengeprint karakter c sebanyak x kali */
{
	while (x>0) {printf("%c",c);x--;}
}

void delApostrof (string *s)
/* menghilangkan semua apostrof di string s*/
{
	string temp;
	int i;
	temp.length=0;
	for (i=0;i<(*s).length;i++){
		if ((*s).s[i]!=39) {
			temp.s[temp.length]=(*s).s[i];
			temp.length++;			
		}
	}
	temp.s[temp.length]='\0';
	*s= temp;
}


void DisplayArea (Sheet S, string PosStart, string PosEnd)
/* menampilkan data pada spreadsheet dengan area (ColAw,RowAw) hingga (RowAk,ColAk) */
{	
		static maxlength [100001];
		address P;
		int i, length, j, RowAw, RowAk,m;
		string s, typee, Pos, ColAw, ColAk;
		
		
		PosToCR(PosStart,&ColAw,&RowAw);
		PosToCR(PosEnd,&ColAk,&RowAk);
		
		
		//-------------------------------//
		//      ISI TABEL MAXLENGTH      //
		//-------------------------------//
		s=ColAw;m=0;
		while ((!IsStringLt(ColAk,s)) || (IsStringEq(ColAk,s)))
		{
			length = 5;
			i = RowAw;
			while (i <= RowAk)
			{
				CRToPos(s,i,&Pos);
				
				GetXYChar(S,Pos,&typee);
					if (!IsStringFormula(typee)) {
						delApostrof(&typee);
						}
					else {
						typee=eval(typee ,S);
						delApostrof(&typee);
						}
						
				if (typee.length != 0)
				{
					if (length <= (typee.length))
					{
						length = (typee.length);
					}
				}
				
				i++;
			}
			m++;
			maxlength[m]=length;
			NextAlfa(&s);
		}
		
		//-------------------------------//
		//    GAMBAR BORDER PALING ATAS  //
		//-------------------------------//
		s = ColAw;m=0;
		PC(201,1);
		PC(205,7);
		PC(203,1);
		while ((!IsStringLt(ColAk,s)) || (IsStringEq(ColAk,s)))
		{
			m++;
			length=maxlength[m];
			PC(205,length+2);
			NextAlfa(&s);
			if(((!IsStringLt(ColAk,s)) || (IsStringEq(ColAk,s)))) {
				PC(209,1);}
			else {
				PC(187,1);
				}
		}
		PC(NEWLINE,1);
		
		//-------------------------//
		//        NAMA KOLOM       //
		//-------------------------//
		//textbackground(1);
		
		s = ColAw;m=0;
		PC(186,1);
		PC(BLANK,7);
		PC(186,1);
		while ((!IsStringLt(ColAk,s)) || (IsStringEq(ColAk,s)))
		{
			m++;
			length = maxlength[m];
			i=(int)((length+2)-(s.length))/2;
			PC(BLANK,i);
			printf("%s",s.s);
			PC(BLANK,(length+2)-i-s.length);
			NextAlfa(&s);
			if(((!IsStringLt(ColAk,s)) || (IsStringEq(ColAk,s)))) {
				PC(179,1);}
			else {
				PC(186,1);
				}
				
		}
		
		//textbackground(0);
		PC(NEWLINE,1);
		
		//---------------------------------------//
		//        BORDER KEDUA PALING ATAS       //
		//---------------------------------------//
		s = ColAw;m=0;
		PC(204,1);
		PC(205,7);
		PC(206,1);
		while ((!IsStringLt(ColAk,s)) || (IsStringEq(ColAk,s)))
		{
			m++;
			length=maxlength[m];
			PC(205,length+2);
			NextAlfa(&s);
			if(((!IsStringLt(ColAk,s)) || (IsStringEq(ColAk,s)))) {
				PC(216,1);}
			else {
				PC(185,1);
				}
		}
		PC(NEWLINE,1);
		
		
		//----------------------//
		//        CONTENT !!    //
		//----------------------//
		
		for (i = RowAw; i <= RowAk; i++)
		{
			//----------------------//
			//       NAMA ROW       //
			//----------------------//
			PC(186,1);
			s=IntToString(i);
			j=(int)((5+2)-(s.length))/2;
			PC(BLANK,j);
			printf("%s",s.s);
			PC(BLANK,(5+2)-j-s.length);
			PC(186,1);
			
			
			//----------------------//
			//        CONTENT !!    //
			//----------------------//
			s = ColAw;m=0;
			while ((!IsStringLt(ColAk,s)) || (IsStringEq(ColAk,s)))
			{
				m++;
				length=maxlength[m];
				
				CRToPos(s,i,&Pos);
				GetXY(S,Pos,&P);
				if (P != Nil)
				{
					//PRINT ISINYA
					CRToPos(s,i,&Pos);
					GetXYChar(S,Pos,&typee);
					if (!IsStringFormula(typee)) {
						//string bukan formula delete apostrof lalu print
						delApostrof(&typee);
						printf("%s",typee.s);
						}
					else {
						//string adalah formula lakukan evaluasi dulu
						typee=eval(typee ,S);
						delApostrof(&typee);
						printf("%s",typee.s);
						}
						
					//spasi setelah content
					PC(BLANK,(length - typee.length)+2);
					if (IsStringEq(ColAk,s)) {
						printf("%c",186);}
					else {
						printf("%c",179);}
				}
				else
				{	/* CELL INI KOSONG */
					for (j = 0; j <= length+1; j++)
					{
						printf("%c",NULLCELL);
					}
					if (IsStringEq(ColAk,s)) {
						printf("%c",186);}
					else {
						printf("%c",179);}
				}
				NextAlfa(&s);
			}
			PC(NEWLINE,1);
			
			//--------------------------//
			//     BORDER CONTENT !!    //
			//--------------------------//
			if (i!=RowAk) {
				PC(199,1);
				PC(196,7);
				PC(215,1);
				s = ColAw;m=0;
				while ((!IsStringLt(ColAk,s)) || (IsStringEq(ColAk,s)))
				{
					m++;
					length=maxlength[m];
					PC(196,length+2);
					if (IsStringEq(ColAk,s)) {
							printf("%c",186);}
						else {
							printf("%c",197);}
					NextAlfa(&s);
				}
				PC(NEWLINE,1);
				}
		}
		
		//----------------------------------//
		//        BORDER PALING BAWAH       //
		//----------------------------------//
		s = ColAw;m=0;
		PC(200,1);
		PC(205,7);
		PC(202,1);
		while ((!IsStringLt(ColAk,s)) || (IsStringEq(ColAk,s)))
		{
			m++;
			length=maxlength[m];
			PC(205,length+2);	
			if (IsStringEq(ColAk,s)) {
				PC(188,1);}
			else {
				PC(207,1);
				}
			NextAlfa(&s);
		}
		PC(NEWLINE,1);
		
}

void DisplayAll (Sheet S)
{
	string MaxIdx;
	string MinIdx, ColMin;
	int RowMin;
	
	RowMin = 1;
	
	ColMin.s[0] = 'A';
	ColMin.s[1] = '\0';
	ColMin.length = 1;
	
	MaxIndex(S,&MaxIdx);
	CRToPos(ColMin, RowMin, &MinIdx);
	
	DisplayArea(S,MinIdx,MaxIdx);
}
